package com.zxwl.obs.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import net.sf.json.JSONObject;

import com.zxwl.base.action.BaseAction;
import com.zxwl.base.dao.QueryParameter;
import com.zxwl.base.service.IBaseService;
import com.zxwl.common.log.ILogger;
import com.zxwl.common.log.LoggerManager;
import com.zxwl.obs.entity.HuaweiHoliday;
import com.zxwl.obs.entity.HuaweiObsUserData;
import com.zxwl.obs.entity.HuaweiTask;
import com.zxwl.obs.service.IHuaweiTaskService;

@Component
public class HuaweiTaskAction extends BaseAction<HuaweiTask>
{

	private static final long serialVersionUID = 5964659622737205154L;
	private final ILogger logger = LoggerManager.getLogger(HuaweiTaskAction.class);
	/**
	 * 1. HuaweiHoliday
	 * 2. HuaweiObsUserData
	 * 3. HuaweiTask
	 */
	public static Map<String, Object> dataMap = new HashMap<String, Object>();
	
	@Resource
	private IHuaweiTaskService huaweiTaskService;

	@Override
	public void fullQueryParameter(QueryParameter queryParameter){}

	@Override
	@Resource
	public void setService(IBaseService<HuaweiTask> huaweiTaskService)
	{
		this.service = huaweiTaskService;
	}
	
	@Override
	public String save()
	{
		try
		{
			logger.info("开始保存任务实体！");
			
			Integer sServiceid = 1;
			List<HuaweiTask> tasks = this.huaweiTaskService.queryList(null);
			if(null != tasks && tasks.size() > 0)
			{
				sServiceid = tasks.size() + 1;
			}
			
			HuaweiTask entity = this.getEntity();
			entity.setsServiceid(String.valueOf(sServiceid));
			
			HuaweiTaskAction.dataMap.put("HuaweiTask", entity);
			
			logger.info("保存任务实体成功，entity:"+JSONObject.fromObject(entity));
		}
		catch (Exception e)
		{
			logger.error("【error】保存任务实体异常！", e);
		}
		return null;
	}
	
	public String saveAll()
	{
		try
		{
			this.logger.info("saveAll--保存所有对象！");
			
			this.huaweiTaskService.saveAll((HuaweiTask)dataMap.get("HuaweiTask"), 
					(HuaweiHoliday)dataMap.get("HuaweiHoliday"), 
					(HuaweiObsUserData)dataMap.get("HuaweiObsUserData"));
			
			this.logger.info("saveAll--保存所有对象成功！");
			this.sendSuccessResult("数据保存成功，等待外呼！");
			return null;
		}
		catch (Exception e)
		{
			this.logger.error("saveAll--保存所有对象异常！", e);
			this.sendErrorResult("数据保存失败，请联系管理员分析错误!");
			return null;
		}
		finally
		{
			dataMap.clear();
			dataMap = new HashMap<String, Object>();
		}
	}
	
	

}
